/**
 * Created by Administrator on 2017/5/11 0011.
 */
//  我的飞机
window.myPlane = {
    ele: null,


    //4.初始化我的飞机
    init: function () {
        this.ele = document.createElement("div");
        gameEngine.ele.appendChild(this.ele);   //创建我的飞机并添加到场景中
        this.ele.className = "myplane";
        this.ele.style.bottom = "0px";
        this.ele.style.left = parseInt((gameEngine.ele.offsetWidth - this.ele.offsetWidth) / 2) + "px";
        return this;
    },

    //8.飞机射击
    send: function (rate) {

        var timer10 = setInterval(function () {
            new Bullet().init().move(0);
        }, rate);
        var timer11;
        var timer12;
        var timer13;
        var timer14;
        var aInput = document.getElementsByTagName("input");
        for (var i = 0; i < aInput.length; i++) {
            aInput[i].index = i;
            aInput[i].onclick = function () {
                for (var j = 0; j < aInput.length; j++) {
                    aInput[j].style.background = "";
                }
                this.style.background = "pink";
                var arr = [-2, -1, 0, 1, 2];
                var arr2 = [-2, -1, 0, 1, 2, 1, 0, -1]
                clearInterval(timer10);
                clearInterval(timer11);
                clearInterval(timer12);
                clearInterval(timer13);
                clearInterval(timer14);
                switch (this.index) {
                    case 0:
                        timer11 = setInterval(function () {
                            new Bullet().init().move(arr[2]);
                        }, rate);
                        break;
                    case 1:
                        timer12 = setInterval(function () {
                            for (var i = 0; i < 5; i++) {
                                new Bullet().init().move(arr[i]);
                            };
                        }, rate);
                        break;
                    case 2:
                        var n=0;
                        timer13 = setInterval(function () {
                            new Bullet().init().move(arr2[(n++) % 8]*2);
                        }, rate / 2);
                        break;
                    case 3:
                        timer14 = setInterval(function () {
                            new Bullet().init().move(arr[parseInt(Math.random() * 5)]);
                        }, rate / 2);
                        break;

                }
            }
        }

    },

    //我的飞机的拖拽方法
    drag: function () {
        var that = this;
        this.ele.onmousedown = function (e) {
            var evt = e || event;
            that.ele.disX = evt.offsetX;
            that.ele.disY = evt.offsetY;


            document.onmousemove = function (e) {
                var evt = e || event;
                evt.preventDefault();
                var x = evt.pageX - that.ele.disX - gameEngine.ele.offsetLeft;
                var y = evt.pageY - that.ele.disY;
                //判断左右
                if (x <= 0) {
                    x = 0;
                } else if (x >= gameEngine.ele.offsetWidth - myPlane.ele.offsetWidth) {
                    x = gameEngine.ele.offsetWidth - myPlane.ele.offsetWidth + "px";
                }
                //判断上下边界
                // if (y <= 0) {
                //     y = 0;
                // } else if (y >= gameEngine.ele.offsetHeight - myPlane.ele.offsetHeight) {
                //     y = gameEngine.ele.offsetHeight - myPlane.ele.offsetHeight + "px";
                // }
                //运动
                that.ele.style.left = x + "px";
                that.ele.style.top = y + "px";


            }

            document.onmouseup = function () {
                document.onmousemove = document.onmouseup = null;
            }
        }

    },

    //我方飞机爆炸
    boom: function () {
        var arr = ["img/me_die1.png", "img/me_die2.png", "img/me_die3.png", "img/me_die4.png"];
        var n = 0;
        var that = this;
        var timer2 = setInterval(function () {
            if (n >= 4) {
                //alert("Game Over");

                window.location.reload(true);
                that.ele.remove();
                clearInterval(timer2);
                var name = prompt("您的分数为: " + gameEngine.scoreDiv.innerHTML + "，请留下您的大名！");
            } else {
                that.ele.style.backgroundImage = "url(" + arr[n++] + ")";
            }
        }, 100);
    }


}

